//
//  ZFMapManager+event.swift
//  map
//
//  Created by fyzq on 2024/2/2.
//

import Foundation
import MapboxMaps
import RxSwift
extension ZFMapManager {
    
//    fileprivate struct ZFMapManagerEventType {
//        static var cancellables: Void?
//    }
//
//    public var cancellables: Set<AnyCancelable> {
//        get {
//            guard let obj = objc_getAssociatedObject(self, &ZFMapManagerEventType.cancellables) as? Set<AnyCancelable> else { return  Set<AnyCancelable>() }
//            return obj
//        }
//        set {
//            objc_setAssociatedObject(self, &ZFMapManagerEventType.cancellables, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
//        }
//    }

    
    func onMapLoaded() -> Observable<MapboxMaps.MapLoaded>{
        return Observable.create { [weak self] observer in
            guard let self = self else {return Disposables.create()}
            mapView?.mapboxMap.onMapLoaded.observe({ payload in
                observer.onNext(payload)
                
            }).store(in: &self.cancellables)
            return Disposables.create()
        }
    }
    func onCameraChanged() -> Observable<CameraChanged>{
        return Observable.create { [weak self] observer in
            guard let self = self else {return Disposables.create()}
            mapView?.mapboxMap.onCameraChanged.observe({ payload in
            
                observer.onNext(payload)
            }).store(in: &self.cancellables)
            return Disposables.create()
        }
    }
    
    func onMapLongPress() -> Observable<MapboxMaps.MapContentGestureContext>{
        return Observable.create { [weak self] observer in
            guard let self = self else {return Disposables.create()}
            mapView?.gestures.onMapLongPress.observe {context in
                observer.onNext(context)
            }.store(in: &cancellables)
            return Disposables.create()
        }
    }

    
}
